class NoticiasController < ApplicationController

  layout 'application'

  uses_tiny_mce(:options => {:theme => 'advanced',
    :browsers => %w{msie gecko},
    :theme_advanced_toolbar_location => "top",
    :theme_advanced_toolbar_align => "left",
    :theme_advanced_resizing => true,
    :theme_advanced_resize_horizontal => false,
    :paste_auto_cleanup_on_paste => true,
    :theme_advanced_buttons1 => %w{bold italic underline strikethrough separator justifyleft justifycenter justifyright separator bullist numlist forecolor backcolor separator link unlink undo redo separator pastetext pasteword},
    :theme_advanced_buttons2 => [],
    :theme_advanced_buttons3 => [],
    :plugins => %w{contextmenu paste}},
    :only => [:novoNoticias, :editarNoticias])

  def index
    listarNoticias
    render :action => 'listarNoticias'
  end

  def listarNoticias
    @noticia_pages, @noticias = paginate :noticias, :order => "postado_em DESC", :per_page => 30
  end

  def novoNoticias
    @page = params[:page]
    @noticia = Noticia.new
  end

  def inserirNoticias
    @page = params[:page]
    @noticia = Noticia.new(params[:noticia])
    
    if @noticia.save
      redirect_to :action => 'listarNoticias', :page => @page
    else
      render :action => "novoNoticias"
    end
  end

  def editarNoticias 
    @page = params[:page]
    @noticia = Noticia.find(params[:id])
  end

  def atualizarNoticias
    @page = params[:page]
    @noticia = Noticia.find(params[:id])
    if @noticia.update_attributes(params[:noticia])
      flash[:notice] = 'Noticia foi editado com sucesso.'
      redirect_to :action => 'verNoticias', :id => @noticia, :page => @page
    else
      render :action => 'editarNoticias'
    end
  end

  def verNoticias
    @page = params[:page]
    @noticia = Noticia.find(params[:id])
  end

  def apagarNoticias
    @page = params[:page]
    Noticia.find(params[:id]).destroy
    redirect_to :action => 'listarNoticias', :page => @page
  end
end
